package org.example.order;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/hello")
public class HelloController {

    @SentinelResource(value = "hello", entryType = EntryType.IN, blockHandler = "helloBlockHandler",
            fallback = "helloFallback")
    @GetMapping
    public String hello() {
        if (System.currentTimeMillis() % 3 == 0) {
            throw new RuntimeException("异常测试！");
        }
        return "hello world!";
    }

    public String helloBlockHandler(BlockException e) {
        return "限流啦！ " + e.getMessage();
    }

    public String helloFallback(Throwable throwable) {
        return "出错降级啦！ " + throwable.getMessage();
    }
}
